package com.gogowan.agroenterprise.supply;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.CommodityRequst;
import com.gogowan.agroenterprise.model.CommodityResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * 获取商品详细信息的异步 搜索收藏推送信息都用到
 * @author Maniac
 *	2012.12.26
 */
public class GetCommodityInfoAsyn extends MyAsyncTask{

	private Context context;
	private ProgressDialog progressDialog;
	
	public GetCommodityInfoAsyn(Context context,ProgressDialog progressDialog) {
		this.context = context;
		this.progressDialog = progressDialog;
	}
	
	@Override
	protected void onPostExecute(Object res) {
		if (res == null || res.equals("")) {
			Toast.makeText(context, context.getString(R.string.server_error), Toast.LENGTH_LONG).show();
		} else {
			
			Message message = (Message) Utils.fromJSON((String) res, Message.class);
			String string = Utils.getMessageContent(message);
			if (string == null || string.equals("")) {
				Toast.makeText(context, context.getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
			} else {
				CommodityResultInfo resultInfo = (CommodityResultInfo)Utils.fromJSON(string, CommodityResultInfo.class);
				int result = resultInfo.getResult();
				if (result == 9010) {
					Intent intent = new Intent(context,CommodityDetailActivity.class);
					intent.putExtra("CommodityDetail", string);
					context.startActivity(intent);
					Utils.randomAnim(context);
				} else {
					Toast.makeText(context, resultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
				}
			}
		}
		progressDialog.dismiss();
		super.onPostExecute(res);
	}

	@Override
	protected Object doInBackground(Object... params) {
		CommodityRequst request = (CommodityRequst)params[0];
		String json = Utils.toJSON(request);
		Message message = new Message(9000, true, json);
		String res = Utils.postCommit(context.getString(R.string.first_url)
				+ context.getString(R.string.first_url_page), message);
		return res;
	}

}
